import data_control from "../../engines/libs/data_control";

const {ccclass, property, menu , disallowMultiple} = cc._decorator;

@ccclass
@menu('工具组件/可拖动物')
export default class DragBase extends cc.Component {
    protected checkTime:number = 1;

    protected origin : cc.Vec2 = null;
    protected nodePos : cc.Vec2 = null;
    protected oldPos:cc.Vec2 = null;
    protected newPos:cc.Vec2 = null;
    protected limit:BroadLimit = {minX:0,maxX:0,minY:0,maxY:0};
    public get Limit(){
        return this.limit;
    }
    protected ctrlNode:cc.Node = null;

    public set NodePos(value:cc.Vec2){
        this.nodePos.x = value.x;
        this.nodePos.y = value.y;
        //console.log("变换了位置");
    }

    protected canMove : boolean = true;
    public CanMove(value : boolean){
        this.canMove = value;
    }
    start(){
        if(null == this.ctrlNode){
            this.ctrlNode = this.node;
        }
        if(this.ctrlNode){
            this.scheduleOnce(this.SetPos);
        }
        
        this.scheduleOnce(()=>{
            let offset : cc.Vec2 = data_control.GetWorldPos(this.ctrlNode.parent);
            //console.log("偏移:",offset.x,offset.y);
    
            this.limit.maxX = (cc.winSize.width*0.5/this.ctrlNode.parent.scaleX)-offset.x;
            this.limit.minX = (-cc.winSize.width*0.5/this.ctrlNode.parent.scaleX)-offset.x;
            this.limit.maxY = (cc.winSize.height*0.5/this.ctrlNode.parent.scaleY)-offset.y;
            this.limit.minY = (-cc.winSize.height*0.5/this.ctrlNode.parent.scaleY)-offset.y;
        },.1);
        if(this.ctrlNode){
            this.RegistEvent();
        }
    }

    SetPos(){
        this.origin = this.ctrlNode.getPosition();
        this.nodePos = this.ctrlNode.getPosition();
    }

    RegistEvent(){
        this.ctrlNode.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.ctrlNode.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onDestroy(){
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        // this.ctrlNode.off(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    onTouchStart(){
    //     cc.tween(this.charSprite).stop();
    //     cc.tween(this.charSprite).to(.1,{scale:1},{easing:'quadIn'}).start();
    //     this.targetFlag = true;
    //     this.inBroad = true;
    //     this.node.width = this.charSprite.width;
    //     this.node.height = this.charSprite.height;
    }
    //触摸移动；
    onTouchMove (event) {
        // console.log("????");
        if(!this.canMove){return;}
        let self = this;
        let touches = event.getTouches();
        //触摸刚开始的位置
        this.oldPos = self.ctrlNode.parent.convertToNodeSpaceAR(touches[0].getStartLocation());
        //触摸时不断变更的位置
        this.newPos = self.ctrlNode.parent.convertToNodeSpaceAR(touches[0].getLocation());
 
        let subPos = this.oldPos.sub(this.newPos);
 
        self.ctrlNode.x = self.nodePos.x - subPos.x;
        self.ctrlNode.y = self.nodePos.y - subPos.y;
        
        // 控制节点移不出屏幕; 
        let nPos = self.ctrlNode.getPosition(); //节点实时坐标；
 
        if (nPos.x < this.limit.minX) {
            nPos.x = this.limit.minX;
        };
        if (nPos.x > this.limit.maxX) {
            nPos.x = this.limit.maxX;
        };
        if (nPos.y < this.limit.minY) {
            nPos.y = this.limit.minY;
        };
        if (nPos.y > this.limit.maxY) {
            nPos.y = this.limit.maxY;
        };
        self.ctrlNode.setPosition(nPos);
    }
    onTouchEnd (e:cc.Event.EventTouch) {
        if(!this.canMove){return;}
        // this.ctrlNode.setPosition(this.origin);
        this.ctrlNode.getPosition(this.nodePos); //获取触摸结束之后的node坐标；
    }
}

export interface BroadLimit{
    minX:number,
    maxX:number,
    minY:number,
    maxY:number
}